﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ImageTagToolViewModel.Models
{
    /// <summary>
    /// This class stores the scanned locations.
    /// </summary>
    public class LocationDatabase
    {
        private static LocationDatabase _Instance;
        public static LocationDatabase Instance
        {
            get
            {
                if (_Instance == null)
                {
                    object locker = new object();
                    lock (locker)
                    {
                        if (_Instance == null)
                            _Instance = new LocationDatabase();
                    }
                }
                return _Instance;
            }
        }

        private List<string> _ScannedLocations;
        public string[] ScannedLocations
        {
            get { return _ScannedLocations.ToArray(); }
        }

        private LocationDatabase()
        {
            _ScannedLocations = new List<string>();
        }

        /// <summary>
        /// Determines if scanning the given location is possible (not already scanned).
        /// </summary>
        /// <param name="path">The location to scan.</param>
        public bool CanAddLocation(string path)
        {
            bool canScan = true;
            foreach (var s in _ScannedLocations)
                if (path.StartsWith(s))
                {
                    canScan = false;
                    break;
                }
            return canScan;
        }

        /// <summary>
        /// Clears all records within this object.
        /// </summary>
        public void Clear()
        {
            _ScannedLocations.Clear();
        }

        /// <summary>
        /// Sets the database identical to the provided collection.
        /// </summary>
        public void InitializeWithRange(IEnumerable<string> collection)
        {
            _ScannedLocations.Clear();
            _ScannedLocations.AddRange(collection);
        }

        /// <summary>
        /// Adds a new entry to the location list.
        /// </summary>
        public void Add(string location)
        {
            _ScannedLocations.Add(location);
        }
    }
}
